﻿using System;
using System.Collections.Generic;
using System.Text;


using CMN.Common;
using CMN.Entity;
using CMN.BLL;
using CMN.Cache;
using CMN.Extends;
using System.IO;
namespace CMN.Extends.Job
{
    public class JobCollcetContent : IJob
    {
        public   JobCollcetContent()
        {

        }
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="obj"></param>
        public void Execute(object obj)
        {
            try
            {
                BLLHelper bll = new BLLHelper();
                QueryParam qp = new QueryParam();
                bool isCollect = false;
                qp.ReturnFields = "*";
                qp.PageIndex = 1;
                qp.PageSize = 10;

                qp.Orderfld = " ";
                int recordNum = 0;
                Extend_CollectLinkInfo bllLinkInfo = new Extend_CollectLinkInfo();
                Extend_CollectSetInfo bllSetInfo = new Extend_CollectSetInfo();
                Extend_CollectContent bllContent = new Extend_CollectContent();
                Module_Article bllArticle = new Module_Article();
                List<Extend_CollectLinkInfoEntity> linkList = null;
                List<Extend_CollectSetInfoEntity> setList = CacheCollcetSet.CollectSetInfoList();
                foreach (Extend_CollectSetInfoEntity setInfo in setList)
                {

                    if (setInfo.Enabled != true)
                    {
                        setInfo.CollectContentTime = DateTime.Now;

                    }
                    // AppLog.Write(setInfo.Name + setInfo.CollectContentTime.ToString(), AppLog.LogMessageType.Debug);
                    if (setInfo.CollectContentTime == null || setInfo.CollectContentTime.Value.AddSeconds(setInfo.ThreadInterval) < DateTime.Now)
                    {

                        setInfo.CollectContentTime = DateTime.Now;
                        qp.Where = string.Concat(" CollectSetId=", setInfo.Id.ToString()," and IsCollect=0 and  CollectNum<4  " );
                        qp.IsReturnCount = false;
                        qp.Orderfld = " ";
                        linkList = bllLinkInfo.List(qp, out recordNum);
                        foreach (Extend_CollectLinkInfoEntity linkInfo in linkList)
                        {
                            isCollect = true;
                            // Extend_CollectLinkInfoEntity linkInfo = linkList[0];

                            CollectHelper chelper = new CollectHelper();
                            Extend_CollectContentEntity content = new Extend_CollectContentEntity();

                            try
                            {
                                try
                                {
                                    if (setInfo.IsPaging)
                                    {
                                        content = chelper.GetPagingContent(setInfo, linkInfo.Url);

                                    }
                                    else
                                        content = chelper.GetContent(setInfo, linkInfo.Url);
                                }
                                catch (Exception ex)
                                {
                                    AppLog.Write(string.Concat("采集内容错误", ex.Message, setInfo.Name.ToString(), ":", linkInfo.Url), AppLog.LogMessageType.Error, ex);
                                    content.TextContent = "";
                                }
                                if (content.TextContent.Length > 20)
                                {
                                    
                                    content.SourceFrom = linkInfo.Url;
                                    content.AddDate = DateTime.Now;
                                    content.ChannelId = setInfo.ChannelId;
                                    content.CollectSetId = setInfo.Id;
                                    content.Enabled = false;
                                    content.IsPass = setInfo.IsAutoUpdate;
                                    content.DataTable_Action = DataTableAction.Insert;
                                    content.Title = content.Title.Replace("'", "").Trim();
                                    if (!bllContent.IsHas(content.Title) && !bllArticle.TitleIsHas(content.Title))
                                        bllContent.InsertUpdateDelete(content);
                                    
                                    linkInfo.IsCollect = true;
                                }
                                else
                                {
                                    linkInfo.CollectNum = 3;
                                    AppLog.Write(string.Concat("采集内容为空", setInfo.Name.ToString(), ":", linkInfo.Url), AppLog.LogMessageType.Debug);
                                }
                                linkInfo.DataTable_Action = DataTableAction.Update;
                                linkInfo.CollectNum = linkInfo.CollectNum + 1;
                                bllLinkInfo.InsertUpdateDelete(linkInfo);
                                //setInfo.CollectContentTime = DateTime.Now;
                            }
                            catch (Exception ex)
                            {
                                AppLog.Write(string.Concat("采集内容错误", setInfo.Name.ToString(), ":", linkInfo.Url), AppLog.LogMessageType.Error, ex);
                            }
                            System.Threading.Thread.Sleep(60);
                        }


                        // AppLog.Write(string.Concat("采集内容----",setInfo.Name.ToString(),":", linkInfo.Id.ToString()), AppLog.LogMessageType.Debug);



                        setInfo.CollectContentTime = DateTime.Now;
                    }
                
                    
                }
               
                if(!isCollect)
                    System.Threading.Thread.Sleep(20000);
            }
            catch (Exception ex)
            {
                AppLog.Write(string.Concat("采集内容错误",ex.Message), AppLog.LogMessageType.Error, ex);
            }
        }
        

        public void Error(Exception e)
        {
            AppLog.Write("采集内容错误", AppLog.LogMessageType.Error, e);
        }
    }
}
